package com.rachel.studyapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.rachel.studyapp.R;
import com.rachel.studyapp.fragment.FunFragment;
import com.rachel.studyapp.fragment.HomeFragment;
import com.rachel.studyapp.fragment.MineFragment;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "zsr";

    private FrameLayout mHomely,mFunly,mMyly;
    private ImageView mHomeicon,mFunicon,mMyicon;
    private TextView mHometext,mFuntext,mMytext;
    private HomeFragment mHomeFragment;
    private Fragment mFunFragment,mMineFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);
        initView();

        //ToolUtils.setStatusBarColor(this,getResources().getColor(R.color.maincolor));

    }



    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }
    public void initView() {

        mHomely = (FrameLayout) findViewById(R.id.home_ly);
        mFunly = (FrameLayout) findViewById(R.id.fun_ly);
        mMyly = (FrameLayout) findViewById(R.id.my_ly);

        mHomely.setOnClickListener(this);
        mFunly.setOnClickListener(this);
        mMyly.setOnClickListener(this);

        mHomeicon = (ImageView) findViewById(R.id.home_img);
        mFunicon = (ImageView) findViewById(R.id.fun_img);
        mMyicon = (ImageView) findViewById(R.id.my_img);

        mHometext = (TextView) findViewById(R.id.home_text);
        mFuntext = (TextView) findViewById(R.id.fun_text);
        mMytext = (TextView) findViewById(R.id.my_text);


        mHomeFragment = new HomeFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_content,mHomeFragment);
        ft.commit();
        selectView(0);

    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()){
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    /**
     * 回去调用fragment 中的这个函数，这里必须重写
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 改变图片和文字的颜色
     *
     * @param position
     */
    private void selectView(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //点之前先去掉之前的选中状态
        resetSelectd();
        resetFragment(ft);
        switch (position) {
            case 0:
                mHomeicon.setImageResource(R.mipmap.icon_home_pager_b);
                mHometext.setTextColor(getResources().getColor(R.color.colorAccent));
                if (mHomeFragment == null){
                    mHomeFragment = new HomeFragment();
                    ft.add(R.id.fragment_content,mHomeFragment);
                }else{
                    ft.show(mHomeFragment);
                }
                break;
            case 1:
                mFunicon.setImageResource(R.mipmap.icon_fun_pager_b);
                mFuntext.setTextColor(getResources().getColor(R.color.colorAccent));
                if (mFunFragment == null){
                    mFunFragment = new FunFragment();
                    ft.add(R.id.fragment_content,mFunFragment);
                }else{
                    ft.show(mFunFragment);

                }
                break;
            case 2:
                mMyicon.setImageResource(R.mipmap.icon_my_pager_b);
                mMytext.setTextColor(getResources().getColor(R.color.colorAccent));
                if (mMineFragment == null){
                    mMineFragment = new MineFragment();
                    ft.add(R.id.fragment_content,mMineFragment);
                }else{
                    ft.show(mMineFragment);


                }
                break;
        }
        ft.commit();
    }

    /**
     * 先恢复图片和文字的初始状态
     */
    private void resetSelectd() {
        mHomeicon.setImageResource(R.mipmap.icon_home_pager);
        mHometext.setTextColor(getResources().getColor(R.color.gray_cc));
        mFunicon.setImageResource(R.mipmap.icon_fun_pager);
        mFuntext.setTextColor(getResources().getColor(R.color.gray_cc));
        mMyicon.setImageResource(R.mipmap.icon_my_pager);
        mMytext.setTextColor(getResources().getColor(R.color.gray_cc));
    }

    /**
     * 隐藏fragment
     * @param ft
     */
    private void resetFragment(FragmentTransaction ft){
        // 用 hide 的方法，不销毁view，相当于压栈
        if (mHomeFragment != null) {
            ft.hide(mHomeFragment);
        }
        if (mFunFragment != null) {
            ft.hide(mFunFragment);
        }
        if (mMineFragment != null) {
            ft.hide(mMineFragment);
        }

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_ly:
                selectView(0);
                break;
            case R.id.fun_ly:
                selectView(1);
                break;
            case R.id.my_ly:
                selectView(2);
                break;


        }
    }


}
